<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-form-item >
      <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="sjrq_begin"></j-date>
      <span class="query-group-split-cust"></span>
      <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="qsjrq_end"></j-date>
    </a-form-item>

  </j-modal>
</template>

<script>

  import { getAction } from '@/api/manage'

  export default {
    name: 'SjrqScopeModal',
    components: {
    },
    data () {
      return {
        model:{
        },
        sjrq_begin: undefined,
        qsjrq_end: undefined,
        title:'选择日期范围',
        width:600,
        visible: false,
        disableSubmit: false
      }
    },
    props: {
      handleAdd: {
        type: Function,
        default: null
      }
    },
    methods: {
      showModal() {
        this.sjrq_begin = undefined;
        this.qsjrq_end = undefined
        //this.params  = params;
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk(e) {

      if(this.sjrq_begin == undefined || this.sjrq_begin == ''){
          this.$message.warning("请选择开始日期!");
          return
        }
       if(this.qsjrq_end != undefined && this.qsjrq_end !=''){
         let oDate1 = new Date(this.sjrq_begin);
         let oDate2 = new Date(this.qsjrq_end);
         if(oDate2 < oDate1){
           this.$message.warning("结束日期不能小于开始日期!");
           return
         }
       }
       if(this.qsjrq_end != undefined && this.qsjrq_end != ''&& this.sjrq_begin != undefined && this.sjrq_begin !=''){
         let dt1 = new Date(this.qsjrq_end.replace(/-/g,"/"));
         let dt2 = new Date(this.sjrq_begin.replace(/-/g,"/"));
         if(dt1.getFullYear()!=dt2.getFullYear() || dt1.getMonth()!=dt2.getMonth()){
           this.$message.warning("结束日期与开始日期不在同一月份！");
           return
         }
       }
        if(this.qsjrq_end == undefined || this.qsjrq_end == ''){
          this.qsjrq_end = this.getCurrentMonthLast()
        }

        let that = this
        this.$confirm({
          title: "确认刷新",
          content: "是否刷新"+this.sjrq_begin+"到"+this.qsjrq_end+"的数据?",
          onOk: function () {
            that.visible = false;
            that.$parent.$parent.loading = true;

            getAction("/llbbRmbCdlc/llbbRmbCdlc/refreshData",{"beginSjrq":that.sjrq_begin,"endSjrq":that.qsjrq_end}).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$parent.$parent.loading = false;
                that.$parent.$parent.loadData(1);
              }else{
                that.$parent.$parent.loading = false;
                that.$message.warning(res.message);
              }
            })
          }
        });

      },
      getCurrentMonthLast(){
    var date=new Date();
    var currentMonth=date.getMonth();
    var nextMonth=++currentMonth;
    var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
    var oneDay=1000*60*60*24;
    var lastTime = new Date(nextMonthFirstDay-oneDay);
    var month = parseInt(lastTime.getMonth()+1);
    var day = lastTime.getDate();
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    return date.getFullYear() + '-' + month + '-' + day;
  },

      submitCallback(){
        this.$emit('ok');
        this.visible = false;
      },
      handleCancel () {
        this.sjrq = '';
        this.close()
      },
    }
  }
</script>